<?php
//
//	file: includes/ucp/ucp_advnot_compose.php
//	begin: 10/07/2006
//	$Author: christopher $
//	$Revision: 28 $
//	$Date: 2008-01-28 23:55:41 +0000 (Mon, 28 Jan 2008) $
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	description: this allows users to email other users.

if ( !defined('IN_PHPBB') )
{
	die('Hack attempt');
}

define('SID_CHECK', true);

$cp_no_menus = true;

// define fields
$fields = array(
	'recipients_to' => array('legend' => 'Recipients_to', 'form_only' => true, 'length_mini' => 3, 'value' => ''),
	'recipients_cc' => array('legend' => 'Recipients_cc', 'form_only' => true, 'value' => ''),
	'recipients_bcc' => array('legend' => 'Recipients_bcc', 'form_only' => true, 'value' => ''),
	'subject' => array('legend' => 'Subject', 'form_only' => true, 'value' => ''),
	'message' => array('type' => 'bbcode', 'legend' => 'Message', 'form_only' => true, 'length_mini' => 3, 'value' => ''),

class user_compose extends form
{
	var $requester;
	var $return_msg;
	var $return_parms;

	function user_compose(&$fields, $requester, $return_msg, $return_parms='')
	{
		global $warning, $warning_msg;
		$warning = $warning_msg = false;

		$this->requester = $requester;
		$this->return_msg = $return_msg;
		$this->return_parms = empty($return_parms) ? array() : $return_parms;

		// init the fields
		parent::form($fields);
	}

	function process()
	{
		$this->init();
		$this->check();
		$this->validate();
		$this->display();
	}

	function check()
	{
		global $error, $error_msg;
		global $warning, $warning_msg;

		$warning = $warning_msg = false;

		// check sid
		if ( SID_CHECK && (!($sid = _read_form('sid', TYPE_NO_HTML)) || ($sid != $user->data['session_id'])) )
		{
			_error('Session_invalid');
		}

		if ( !_button('submit_form') )
		{
			return;
		}

		parent::check();

		// halt on error
		if ( $error )
		{
			_warning($error_msg);
			$error = $error_msg = false;
		}
	}

	function validate()
	{
		global $template, $config, $user, $forums;
		global $error, $error_msg;
		global $warning, $warning_msg;

		if ( !_button('submit_form') || $warning || $error )
		{
			return;
		}

		$fields = array(
			'recipients_to' => $this->fields['recipients_to']->value,
			'recipients_cc' => $this->fields['recipients_cc']->value,
			'recipients_bcc' => $this->fields['recipients_bcc']->value,
			'message' => $this->fields['message']->value,
			'subject' => $this->fields['usubject']->value,
		);

		foreach ( $this->fields as $field_name => $field )
		{
			$this->fields[$field_name]->validate();
			if ( !empty($field->data['field']) )
			{
				$fields[ $field->data['field'] ] = $field->value;
			}
			if ( !empty($field->data['sub_fields']) && is_array($field->data['sub_fields']) )
			{
				foreach ( $field->data['sub_fields'] as $sub_field_name => $sub_field_table_field )
				{
					$fields[$sub_field_table_field] = $field->data['sub_values'][$sub_field_name];
				}
			}
		}

		// prepare the message
		$notify = new advnot_parent('email');
		$notify->add_users($fields['recipients_to'], 'to');
		$notify->add_users($fields['recipients_cc'], 'cc');
		$notify->add_users($fields['recipients_cc'], 'bcc');
		$notify->prepare(sprintf($user->lang('Email_Subject'), $fields['subject']) , $fields['message']);

		// send the message
		if ($notify->send())
		{
			message_return('Message_sent');
		}
		else
		{
			_warning('Message_failed');
		}

	}

	function display()
	{
		global $template, $user;
		global $warning, $warning_msg;

		if ( $warning )
		{
			$template->assign_block_vars('warning', array(
				'WARNING_TITLE' => $user->lang('Information'),
				'WARNING_MSG' => $warning_msg,
			));
		}
		_hide('sid', $user->data['session_id']);

		parent::display();
	}
}

// cancel pressed
if ( _button('cancel') )
{
	redirect($config->url('usercp', '', true));
}

// instantiate the form
$navigation->clear();
$navigation->add($cp_panels->data[ $menus[$menu_id] ]['panel_name'], '', $cp_requester, $cp_parms + array('mode' => $menu_id));
$parms = array(
	'mode' => $menu_id,
	'sub' => $subm_id == $menu_id ? '' : $subm_id,
	'ctx' => $ctx_id == $subm_id ? '' : $ctx_id,
);

$form = new user_compose($fields, $cp_requester, 'Click_return_' . $menu_id, $cp_parms + $parms);
$form->process();
$template->set_switch('form');
$template->set_filenames(array('body' => 'form_body.tpl'));

?>